Quiz: Flow Control

To View Tricks: Login Required

Number of Questions: 15

Question: 1 -

Which statement is true?

public void test(int x)
{
	int odd = 1;
	if(odd) /* Line 4 */
	{
		System.out.println("odd");
	}
	else
	{
		System.out.println("even");
	}
}

Options:
  1. Compilation fails.

  2. "odd" will always be output.

  3. "even" will always be output.

  4. "odd" will be output for odd values of x, and "even" for even values.

  5. Answer:

    Compilation fails.

    Solution:

    The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer.


Question: 2 -

What will be the output of the following code snippet?+

int a=15;
int b=25;
if ((a < b ) || ( a = 5)>15)
	system.out.println(a);
else
	system.out.println(b);

Options:
  1. No output

  2. Error

  3. 15

  4. 25

  5. Answer:

    15

    Solution not available.

Question: 3 -

Which statement is true?

public class While
{
	public void loop()
	{
		int x= 0;
		while ( 1 ) /* Line 6 */
		{
			System.out.print("x plus one is " + (x + 1)); /* Line 8 */
		}
	}
}

Options:
  1. There is a syntax error on line 1.

  2. There are syntax errors on lines 1 and 6.

  3. There are syntax errors on lines 1, 6, and 8.

  4. There is a syntax error on line 6.

  5. Answer:

    There is a syntax error on line 6.

    Solution not available.

Question: 4 -

Which two are acceptable types for x?

switch(x)
{
	default:
		System.out.println("Hello");
}

Options:
  1. short

  2. char

  3. float

  4. long

  5. Answer:

    short

    Solution not available.

Question: 5 -

What will be the output of the program?

int x, y;
x=15; y=20;
if (x>15)
if(y>15)
{
	system.ptintln("y is "+y);
}
else
	system.out.ptintln("x is "+x);

Options:
  1. No output

  2. x is 15

  3. Error

  4. y is 20

  5. Answer:

    x is 15

    Solution not available.